#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include<unistd.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>

void ditui(int n)
{
    if (n>0)
    {
        for (int i=0;i<n;i++)
        {
            printf("\t");
        }
        ditui(n-1);
        printf("地市第%d次递推\n",n);
    }

}

void digui(int n,int m)
{
    if (n<m)
    {
        for (int i=0;i<n;i++)
        {
            printf("\t");
        }
        ditui(n+1);
        printf("地市第%d次递推\n",n);
    }

}
void travelDir(char* pathName);
int main(int argc,char* argv[])
{


    if (argc<2)
    {
        printf("请输入要遍历的目录名\n");
        exit(-1);
    }
    travelDir(argv[1]);

    return 0;
}

void travelDir(char* pathName)
{
    char filename[256];
    printf("打开目录%s\n",pathName);
    DIR* pDir =  opendir(pathName);
    if (NULL==pDir)
    {
        perror("opendir失败");
        return;
    }

    printf("打开目录成功\n");
    int num = 0;
    while (1)
    {
        num=num+1;
        struct  dirent* pd = readdir(pDir);
        if (NULL == pd)
        {
            break;
        }

        sprintf(filename,"%s/%s",pathName,pd->d_name);
        // printf("这是%s里%d个文件%s\n",pathName,num+1,pd->d_name);
        if (pd->d_type==DT_DIR)
        {
            printf("---%d:%s\n",num,filename);
            if (!(strcmp(".",pd->d_name) || strcmp("..",pd->d_name)))
            {
                travelDir(filename);
            }

        }else
        {
            printf("%d:%s\n",num,filename);

        }
    }
    printf("遍历完毕\n");
    closedir(pDir);
}